package com.devframe.web.taglib;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

/**
 * 
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;SelectTag&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --分页控制脚本输出--&lt;/b&gt;
 * </pre>
 * <pre>
 * </pre>
 * <pre>
 * &lt;b&gt;--样例--&lt;/b&gt;
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class PageParamsTag extends AbstractBaseTag {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7993237667875479682L;

    /**
     * 标签处理逻辑，未完成...................
     * 
     * @return SKIP_BODY
     */
    public int doStartTag() {

        return SKIP_BODY;
    }

    /**
     * doEndTag
     * 
     * @return EVAL_PAGE
     * @throws JspException
     *             JspException
     */
    public int doEndTag() throws JspException {
        try {
            HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
            
            Enumeration enu = request.getParameterNames();
            StringBuffer pageUrl = new StringBuffer();
            while (enu.hasMoreElements()) {
                String paramName = (String) enu.nextElement();
                // if (paramName.indexOf("pageInfo")<0) {
                String[] paramValues = request.getParameterValues(paramName);
                if (paramValues != null) {
                    for (int i = 0; i < paramValues.length; i++) {
                        if (pageUrl.length() > 0) {
                            pageUrl.append("&");
                        }
                        pageUrl.append(paramName).append("=").append(paramValues[i]);
                    }
                }
            }
            pageUrl.insert(0, "?");
            pageUrl.insert(0, request.getRequestURI());
            JspWriter writer = null;
            try {
                writer = this.getPreviousOut();
            } catch (Exception e1) {
                writer = pageContext.getOut();
            }

            try {
                writer.print(pageUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return EVAL_PAGE;
        } catch (Exception e) {
            return EVAL_PAGE;
        }
    }

    /**
     * release
     * 
     * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
     */
    public void release() {
        super.release();
    }

}
